<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>年级/班级管理</title>
    <link rel="stylesheet" href="/plug/layui/css/layui.css">
</head>
<body>
<div style="margin: 20px">
    <div class="layui-inline">
        <button class="layui-btn layui-btn-normal" type="button" onclick="edit('添加')">添加</button>
    </div>
    <div id="treeTable"></div>
</div>
<script src="/js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/plug/layui/layui.all.js"></script>
<script>
    $("input[type=reset]").click(function () {
        $(".layui-form select").val("");
    });
    layui.config({
        base: '/plug/layui/lay/modules/'
    }).use(['treeTable'], function () {
        let treeTable = layui.treeTable;
        treeTable.render({
            elem: '#treeTable',
            url: '/gradeClass/getList',
            tree: {
                iconIndex: 0,
                isPidData: true,
                idName: 'id',
                pidName: 'parentId'
            },
            cols: [
                {field: 'name', title: '年级/班级'},
                {field: 'deep', title: '深度', align: 'center'},
                {field: 'sort', title: '排序', align: 'center'},
                {field: 'createTime', title: '创建时间', align: 'center'},
                {width: '200px', title: '操作', templet: function (obj) {
                        return `<button class="layui-btn layui-btn-warm" onclick="edit('修改', ${obj.id})">修改</button>
                                <button class="layui-btn layui-btn-danger" onclick="del(${obj.id})">删除</button>`
                    }
                }
            ]
        });
    });
    layui.use(['form', 'laypage'], function() {
        const form = layui.form;
        form.render('select');
    });

    function edit(title, id) {
        layer.open({
            title: title,
            type: 2,
            area: ['800px', '500px'],
            fixed: false, //不固定
            content: "/gradeClass/edit?id=" + (id === undefined ? '' : id)
        });
    }

    function del(id) {
        layer.confirm('确认要删除吗？', {
            btn: ['确定', '取消'] //按钮
        }, function() {
            $.post("/gradeClass/del", { id: id }, function (rest) {
                layer.msg(rest.msg);
                if(rest.code === 200) {
                    location.reload();
                }
            });
        });
    }
</script>
</body>
</html>